import { request } from "../../request/index.js"
// pages/order/order.js
Page({
  data: {
    tabs: [{
      id: 0,
      value: "全部订单",
      isActive: true
    }, {
      id: 1,
      value: "待付款",
      isActive: false
    }, {
      id: 2,
      value: "待收货",
      isActive: false
    }, {
      id: 3,
      value: "退款退货",
      isActive: false
    }],
    //订单列表
    orders: []
  },
  onShow() {
    //判断token
    const token = wx.getStorageSync("token");
    if (!token) {
      wx.navigateTo({ url: '/pages/auth/auth' });
      return;
    }

    let currentPages = getCurrentPages()
    //通过页面栈 获取到url里面的链接
    const { type } = currentPages[currentPages.length - 1].options
    this.getOrders(type)
    this.changeTitleByIndex(type - 1)
  },
  //获取订单列表
  async getOrders(type) {
    let res = await request({ url: "/my/orders/all", data: { type } })
    let { orders } = res.data.message
    orders = orders.map(v=>{
      return {...v,create_time_cn:(new Date(v.create_time*1000).toLocaleString())}
    })
    this.setData({
      orders
    })
  },
  //顶部索引active
  tabsItemChange(e) {
    const { index } = e.detail
    this.changeTitleByIndex(index)
    // 重新发送请求
    this.getOrders(index + 1)
  },
  //根据标题索引来激活选中
  changeTitleByIndex(index) {
    let { tabs } = this.data
    tabs.forEach(v => {
      v.id == index ? v.isActive = true : v.isActive = false
    });
    this.setData({
      tabs
    })
  }



})